<?php
/**
 * AJAX Handler for Tags: List - list all tags for this project and send them to the ajax.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		
		$Project->instanciate();
		$Tags = $Project->getTags();
		$js_tag_index = "";
		$js_tag_i = 0;
		$js_tag_name = "";
		
		foreach($Tags as $key => $value) {
		    $thisTag =& $TagStash->get($value);
		    
		    $js_tag_index .= "tag_id[{$js_tag_i}] = '{$thisTag->getID()}';\n";
		    $js_tag_name .= "tag_name[{$js_tag_i}] = '{$thisTag->getName()}';\n";
		    
		    $js_tag_i++;
		}
		echo <<<EOHTML

		    <!DOCTYPE html>
		    <html>
			<head><title></title></head>
			<body>
			<script type="text/javascript">
			    var tag_id = new Array();
			    var tag_name = new Array();
			    {$js_tag_index}
			    {$js_tag_name}
				parent.TagsEDJX.updateTags(tag_id, tag_name);
			</script>
			    
			</body>
		    </html>
		
EOHTML;
		
	    } else {
		throw new InvalidProjectException('');
	    }
	    
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die('');
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
?>
